﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SPLinkedIn.SPLinkedInWP
{
    class SPListOps
    {
        public SPListOps() { }

        public void SPListAddItems(string sTitle, string sValue)
        {
            bool bExists = false;

            SPWeb sWeb = SPContext.Current.Web;

            SPList sList = sWeb.Lists["LinkedInData"];

            SPListItemCollection listItems = sList.Items;
            int itemCount = listItems.Count;

            for (int k = 0; k < itemCount; k++)
            {
                try
                {
                    SPListItem item = listItems[k];
                    if (item["Title"].ToString().Equals(sTitle))
                        bExists = true;
                }
                catch (Exception x)
                {

                }
            }

            if (!bExists)
            {
                SPListItem sItem = sList.AddItem();
                sItem["Title"] = sTitle;
                sItem["Value"] = sValue;
                sItem.Update();
            }
            return;
        }

        public void SPListClearListItems()
        {
            SPWeb sWeb = SPContext.Current.Web;

            SPList sList = sWeb.Lists["LinkedInData"];

            SPListItemCollection listItems = sList.Items;
            int itemCount = listItems.Count;

            

            for (int k = 0; k < itemCount; k++)
            {
                try
                {
                    SPListItem item = listItems[0];
                    //listItems.Delete(k);
                    item.Delete();
                }
                catch (Exception x)
                {

                }
            }

            return;
        }

        public string SPListGetItemValue(string sTitle)
        {
            string sValue = string.Empty;

            SPWeb sWeb = SPContext.Current.Web;

            SPList sList = sWeb.Lists["LinkedInData"];

            foreach (SPListItem sItem in sList.Items)
            {
                if (sItem["Title"].ToString().Equals(sTitle))
                    sValue = sItem["Value"].ToString();
            }

            return sValue;
        }

    }
}
